package org.droid.campusorder;

import java.util.ArrayList;

import org.droid.R;
import org.droid.campusorder.util.MyDBHelperAboutSearch;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class SearchActivity extends MyActivity {
	private Button btn_search;
	private Button btn_clear;
	private ListView lv_search;
	private EditText et_search;
	private MyDBHelperAboutSearch dbHelper;
	private String searchContent;
	private Cursor c;
	private ArrayList<String> listTemp = new ArrayList<String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		dbHelper = new MyDBHelperAboutSearch(getApplicationContext(), "SearchContentDB");
		initWidget();
		listViewRefresh();
		setListener();
	}
	
	private void setListener(){
		btn_clear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dbHelper.deleteAllData();
				myHandler.sendEmptyMessage(0);
				listTemp.clear();
				et_search.setText("");
			}
		});

		btn_search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				searchContent = et_search.getText().toString().trim();
				if (!searchContent.equals("") && isSingle(searchContent)) {
					dbHelper.insert(searchContent);
					myHandler.sendEmptyMessage(0);
					Toast.makeText(getApplicationContext(), "search!",
							Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(getApplicationContext(), "nosearch!",
							Toast.LENGTH_LONG).show();
				}
			}
		});

		lv_search.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				getHistoryData();
				et_search.setText("");
				et_search.append(listTemp.get(position));
			}
		});
	}

	private void initWidget() {
		btn_search = (Button) findViewById(R.id.btn_search);
		btn_clear = (Button) findViewById(R.id.btn_clear);
		et_search = (EditText) findViewById(R.id.et_search);
		lv_search = (ListView) findViewById(R.id.lv_search);
	}

	Handler myHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			// 生成适配器，数组
			listViewRefresh();
		}
	};

	/** 刷新listview的布局 */
	public void listViewRefresh() {
		c = dbHelper.query();
		HideClearButtonOrNot();
		SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
				getApplicationContext(), R.layout.search_listitem,// ListItem的XML实现
				c,// 数据来源
				new String[] { "content" },// 动态数组与ListItem对应的子项
				new int[] { R.id.ItemContent });// ListItem的XML文件里面的两个TextView
												// ID
		lv_search.setAdapter(simpleCursorAdapter);
	}

	private void getHistoryData() {
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			int contentColumn = c.getColumnIndex("content");
			String content = c.getString(contentColumn);
			if (!listTemp.contains(content)) {
				listTemp.add(content);
			}
		}
	}

	/** 判断EditView输入的值是否重复 */
	private boolean isSingle(String s) {
		boolean b = true;
		getHistoryData();
		if (listTemp.contains(s)) {
			b = false;
		}
		return b;
	}

	/** 判断是否要隐藏clear按键 */
	private void HideClearButtonOrNot() {
		if (c.isAfterLast()) {
			btn_clear.setVisibility(View.GONE);
		} else {
			btn_clear.setVisibility(View.VISIBLE);
		}
	}

}